﻿using System;
using Training.Library.DataAccess.Administration.Catalog;
using System.Data;
using Microsoft.Reporting.WebForms;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Training.Library.DataAccess.Administration.Catalog;
using System.Configuration;
using Microsoft.Reporting.WebForms;
using Training.Library.DomainObject.Administration.Catalog;


namespace Training.Web.Reports 
{

    public partial class rptPesertaList : BasePage
    {
        private CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlCabang.Items.Clear();
                List<Cabang> list = CabangDal.GetList();
                Cabang objCabang = CabangDal.GetById(base.CabangID);
                if (objCabang != null)
                {
                    if (!objCabang.NamaCabang.Trim().ToLower().Contains("pusat"))
                    {
                        var query = list.Where(x => x.CabangID == base.CabangID).OrderBy(x => x.NamaCabang);
                        list = query.ToList();

                        ddlCabang.Items.Insert(0, new ListItem("-- pilih cabang --", string.Empty));
                    }
                    else
                    {
                        var query = list.OrderBy(x => x.NamaCabang);
                        list = query.ToList();

                        ddlCabang.Items.Insert(0, new ListItem("-- semua cabang --", string.Empty));
                    }

                }

                //var query = list.OrderBy(x => x.NamaCabang);
                //list = query.ToList();
                //ddlCabang.Items.Clear();

                list.ForEach(delegate(Cabang obj)
                {
                    ddlCabang.Items.Add(new ListItem(obj.NamaCabang, obj.CabangID.ToString()));
                });

                

               

            }
        }

        protected void btnProses_Click(object sender, EventArgs e)
        {

            Cabang objCabang = CabangDal.GetById(base.CabangID);
            if (objCabang != null)
            {
                if (!objCabang.NamaCabang.Trim().ToLower().Contains("pusat"))
                {
                    if(ddlCabang.SelectedIndex==0)
                    {
                    ClientScript.RegisterClientScriptBlock(typeof(Page), "success", "alert('" + "Pilih cabang sebagai parameter laporan !" + "');", true);
                    return;
                    }
                }
                
            }

            int cabangID = 0;
            int lokasiID = 0;

            if (ddlCabang.SelectedIndex != 0)
                cabangID = Convert.ToInt16(ddlCabang.SelectedItem.Value);

            //if (ddlLokasi.SelectedIndex != 0)
            //    lokasiID = Convert.ToInt16(ddlLokasi.SelectedItem.Value);

            KaryawanRepo KaryawanDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
            DataTable dt = KaryawanDal.GetDataTableReport(cabangID, lokasiID);

            rsViewer.Reset();
            rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/rptPeserta.rdlc");
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "DataSet1";
            rds.Value = dt;

            rsViewer.LocalReport.DataSources.Clear();
            rsViewer.LocalReport.DataSources.Add(rds);
            rsViewer.LocalReport.Refresh();
        }

        protected void ddlCabang_SelectedIndexChanged(object sender, EventArgs e)
        {
            //LokasiRepo LokasiDal = new LokasiRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
            //List<Lokasi> list = LokasiDal.GetList();
            //var query = list.Where(x=>x.CabangID==Convert.ToInt32(ddlCabang.SelectedItem.Value)).OrderBy(x => x.NamaLokasi);
            //list = query.ToList();
            //ddlLokasi.Items.Clear();

            //list.ForEach(delegate(Lokasi obj)
            //{
            //    ddlLokasi.Items.Add(new ListItem(obj.NamaLokasi, obj.LokasiID.ToString()));
            //});

            //ddlLokasi.Items.Insert(0, new ListItem("-- semua lokasi --", string.Empty));


        }
}
}